﻿using System;
using System.Net;
using System.Windows.Threading;
using TweetSharp;

namespace PocketBlueBird.ViewModels
{
    public class LoginViewModel : BaseViewModel
    {
        private TwitterService _service;

        public OAuthRequestToken RequestToken { get; set; }

        public TwitterUser _userInfo;

        public TwitterUser UserInfo
        {
            get { return _userInfo; }
            set
            {
                if (value != _userInfo)
                {
                    _userInfo = value;
                    NotifyPropertyChanged("UserInfo");
                }
            }
        }

        public LoginViewModel(Action<OAuthRequestToken, TwitterResponse> action)
        {
            _service = new TwitterService(_consumerKey, _consumerSecret);
            _service.GetRequestToken(action);
        }

        public LoginViewModel(Dispatcher dispatcher)
        {
            _service = new TwitterService(_consumerKey, _consumerSecret,
                App.AccessToken.Token, App.AccessToken.TokenSecret);

            _service.GetUserProfile((userInfo, response) =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    dispatcher.BeginInvoke(() =>
                    {
                        App.UserInfo = userInfo;
                        this.UserInfo = userInfo;
                    });
                }
                else
                {
                    throw new Exception(response.StatusCode.ToString());
                }
            });
        }

        public void GetAccessToken(string verifier, Action<OAuthAccessToken, TwitterResponse> action)
        {
            _service.GetAccessToken(
                          this.RequestToken,
                          verifier,
                          action
                          );
        }

        public Uri GetAuthorizationUri()
        {
            return _service.GetAuthorizationUri(this.RequestToken);
        }

        public void GetUserInfo(Dispatcher dispatcher)
        {
            _service.GetUserProfile((user, response) =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    dispatcher.BeginInvoke(() =>
                    {
                        this.UserInfo = user;
                        App.UserInfo = user;
                    });
                }
                else
                {
                    throw new Exception(response.StatusCode.ToString());
                }
            });
        }

        public void AuthenticateWith()
        {
            _service.AuthenticateWith(
                _consumerKey, _consumerSecret,
                App.AccessToken.Token, App.AccessToken.TokenSecret
                );
        }

        internal void LogOut()
        {
            App.AccessToken = new OAuthAccessToken() { Token = null };
            this.UserInfo = null;

            App.TweetsDic.Clear();
            App.MyTweetsDic.Clear();
            App.MentionsDic.Clear();
            App.FollowersDic.Clear();
            App.FriendsDic.Clear();
        }
    }
}
